package word.attribute;

import org.json.simple.JSONObject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 예문 속성
 */
public class ExampleAttribute implements AdditionalAttribute
{
	static
	{
		AttributeList.addAttribute(new ExampleAttribute(null, null));
	}

	private String example;     // 예문내용
	private String exampleMean; // 예문해석

	private static final Font exampleFont = new Font("맑은 고딕", Font.PLAIN, 12); // 출력될 폰트 설정

	/**
	 * 생성자
	 * @param example
	 * @param exampleMean
	 */
	public ExampleAttribute(String example, String exampleMean)
	{
		this.example = example;
		this.exampleMean = exampleMean;
	}

	@Override
	public JPanel toPanel()
	{
		JPanel panel = new JPanel();

		// add()할 때마다 한 줄씩 배치되도록 레이아웃 설정.
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setOpaque(false);

		// [예문] 라벨
		JLabel examLabel = new JLabel("[예문]");
		examLabel.setFont(exampleFont);
		panel.add(examLabel);

		// 예문 라벨
		JLabel exampleLabel = new JLabel(example);
		exampleLabel.setFont(exampleFont);
		panel.add(exampleLabel);

		// 예문 해석 라벨
		JLabel exampleMeanLabel = new JLabel(exampleMean);
		exampleMeanLabel.setFont(exampleFont);
		panel.add(exampleMeanLabel);

		return panel;
	}

	@Override
	public AdditionalAttribute createAttributeByDialog(JFrame owner)
	{
		JDialog dialog = new JDialog(owner, getName() + " 추가", true)
		{
			{
				setSize(300, 161);
				setLayout(null);
				setLocationRelativeTo(null);
				setResizable(false);

				// 예문 라벨
				JLabel txt_example = new JLabel("[예문] ");
				txt_example.setBounds(25, 20, 100, 25);
				add(txt_example);

				// 예문 입력 필드
				final JTextField edit_example = new JTextField();
				edit_example.setBounds(65, 20, 200, 25);
				add(edit_example);

				// 예문 해석 라벨
				JLabel txt_exampleMean = new JLabel("[해석] ");
				txt_exampleMean.setBounds(25, 50, 100, 25);
				add(txt_exampleMean);

				// 예문 해석 입력 필드
				final JTextField edit_exampleMean = new JTextField();
				edit_exampleMean.setBounds(65, 50, 200, 25);
				add(edit_exampleMean);

				// 확인버튼 추가
				JButton btn_ok = new JButton("확인");
				btn_ok.setBounds(70, 90, 70, 30);
				btn_ok.addActionListener(new ActionListener()
				{
					/**
					 * 확인버튼 눌렀을 때 호출되는 메서드
					 * @param e
					 */
					@Override
					public void actionPerformed(ActionEvent e)
					{
						example = edit_example.getText();
						exampleMean = edit_exampleMean.getText();
						dispose();
					}
				});
				add(btn_ok);
				// 취소버튼 추가
				JButton btn_cancel = new JButton("취소");
				btn_cancel.setBounds(150, 90, 70, 30);
				btn_cancel.addActionListener(new ActionListener()
				{
					/**
					 * 취소버튼 눌렀을 때 호출되는 메서드
					 * @param e
					 */
					@Override
					public void actionPerformed(ActionEvent e)
					{
						example = null;
						exampleMean = null;
						dispose();
					}
				});
				add(btn_cancel);
			}
		};
		// 확인 버튼 눌렀으면
		dialog.setVisible(true);
		if (example != null && exampleMean != null)
			return new ExampleAttribute(example, exampleMean);
		else
			return null;
	}

	@Override
	public JSONObject toJsonObject()
	{
		JSONObject object = new JSONObject();
		object.put("example", example);
		object.put("exampleMean", exampleMean);

		return object;
	}

	@Override
	public AdditionalAttribute createAttributeByJSON(JSONObject object)
	{
		String example = (String) object.get("example");
		String exampleMean = (String) object.get("exampleMean");

		if (example != null && exampleMean != null)
			return new ExampleAttribute(example, exampleMean);

		else
			return null;
	}

	@Override
	public String getName()
	{
		return "[예문]";
	}
}